﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using BTKERP.Common.ConfigHelper;

namespace BTKERP.DAL.DataAccess
{
    public class Connection
    {
        public string ConnectionName { get; set; }
        public string ConnectionString
        {
            get
            {
                if (ConnectionInfo != null)
                {
                    return ConnectionInfo.ConnectionString;
                }
                return string.Empty;
            }
            set
            {
                ConnectionInfo = new SqlConnection(value);
            }
        }
        public bool IsUsing { get; set; }
        public SqlConnection ConnectionInfo { get; set; }

        public Connection Next { get; set; }
        static string _ConnStr; 

        static Connection()
        {
            _ConnStr = SystemConfigHelper.GetConnectionString("SQLConnString1");
        }

        public Connection()
        {
            ConnectionInfo = new SqlConnection(_ConnStr);
        }

        public void Open()
        {
            if (ConnectionInfo.State != ConnectionState.Open)
            {
                int times = 0;
                while (times < 5) // try 5 times 
                {
                    try
                    {
                        ConnectionInfo.Open();
                        break;
                    }
                    catch (Exception e)
                    {
                        ConnectionInfo = new SqlConnection(_ConnStr);
                        times++;
                    }
                }
            }
        }

        public void Close()
        {
            if (ConnectionInfo.State == ConnectionState.Open)
            {
                ConnectionInfo.Close();
            }
        }
    }

}

